package com.china08.yxyapi.api.manage.tec.teaching.lecture.prepare;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack.Res;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.mg.lecture.YxyLecturePrePackRepository;
import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.YxUserPointsUtilV3;

/**
 * 
 * 挑选备课包--平台备课包--引用备课包
 *
 */
@CrossOrigin
@RestController
public class LectureUptUsedPackController extends YxyApiBaseController {

    @Autowired
    private YxyLecturePrePackRepository yxyLecturePrePackRepository;

    @Autowired
    private YxUserPointsUtilV3 yxUserPointsUtilV3;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/usedpack/upt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String userId = getProfile(request, response).getId();
        String userNick = reqModel.userNick;
        Long packSize = reqModel.packSize;
        String schoolId = getProfile(request, response).getSchoolId();
        String schoolNick = reqModel.schoolNick;
        String areaIdCity = getProfile(request, response).getSchoolAreaIdCity();
        String district = getProfile(request, response).getSchoolAreaIdDistrict();
        String province = getProfile(request, response).getSchoolAreaIdProvince();
        String id = reqModel.id;
        YxyLecturePrePack pack = yxyLecturePrePackRepository.findById(id);
        yxUserPointsUtilV3.cainaBackpack(pack.getUserId());
        List<YxyLecturePrePack> lYxyLecturePrePacks = yxyLecturePrePackRepository
                .findByUserIdAndSchoolIdAndPackNameAndIsDeleteNot(userId, schoolId, pack.getPackName(), 1);
        int i = 0;
        boolean isPack = false;
        if (lYxyLecturePrePacks != null && lYxyLecturePrePacks.size() > 0) {
            for (YxyLecturePrePack yxyLecturePrePack : lYxyLecturePrePacks) {
                if (yxyLecturePrePack.getResources().size() == pack.getResources().size()) {
                    for (Res res : yxyLecturePrePack.getResources()) {
                        for (Res res1 : pack.getResources()) {
                            if (res.getResourceId().equals(res1.getResourceId())) {
                                i++;
                                break;
                            }
                        }
                    }
                    if (yxyLecturePrePack.getResources().size() == i) {
                        isPack = true;
                        break;
                    } else {
                        isPack = false;
                    }
                } else {
                    continue;
                }
            }
        }
        RespModel model = new RespModel();
        if (!isPack) {
            pack.setUsedCount(pack.getUsedCount() + 1l);
            YxyLecturePrePack lecturePrePack = new YxyLecturePrePack();
            lecturePrePack.setAreaIdCity(areaIdCity);
            lecturePrePack.setAreaIdDistrict(district);
            lecturePrePack.setAreaIdProvince(province);
            lecturePrePack.setBookcontentsId(pack.getBookcontentsId());
            lecturePrePack.setBookcontentsName(pack.getBookcontentsName());
            lecturePrePack.setCreatedBy(pack.getCreatedBy());
            lecturePrePack.setIsYx(0);
            lecturePrePack.setPackName(pack.getPackName());
            lecturePrePack.setPhaseId(pack.getPhaseId());
            lecturePrePack.setPhaseName(pack.getPhaseName());
            lecturePrePack.setPv(pack.getPv());
            lecturePrePack.setPackSize(packSize * 1024);
            lecturePrePack.setResources(pack.getResources());
            lecturePrePack.setSchoolId(schoolId);
            lecturePrePack.setSchoolNick(schoolNick);
            lecturePrePack.setSemesterId(pack.getSemesterId());
            lecturePrePack.setSemesterName(pack.getSemesterName());
            lecturePrePack.setShare(0);
            lecturePrePack.setStatus(1);
            lecturePrePack.setSubjectId(pack.getSubjectId());
            lecturePrePack.setSubjectName(pack.getSubjectName());
            lecturePrePack.setUsedCount(0l);
            lecturePrePack.setUserId(userId);
            lecturePrePack.setUserNick(userNick);
            lecturePrePack.setVersionId(pack.getVersionId());
            lecturePrePack.setVersionName(pack.getVersionName());
            lecturePrePack.setIsDelete(0);
            lecturePrePack.setReferencUserId(reqModel.userId);
            lecturePrePack.setReferencId(id);
            yxyLecturePrePackRepository.save(lecturePrePack);
            yxyLecturePrePackRepository.save(pack);
            model.createdDateFormat = DateToolUtils.format(pack.getLastModifiedDate(), DateToolUtils.FORMAT_SHORT_YMD);
            model.id = lecturePrePack.getId();
            List<ResModel> resModels = new ArrayList<ResModel>();
            for (Res respModel : pack.getResources()) {
                ResModel resModel = new ResModel();
                resModel.formatId = respModel.getFormatId();
                resModel.title = respModel.getResourceName();
                resModels.add(resModel);
            }
            model.resources = resModels;
            model.schoolNick = lecturePrePack.getSchoolNick();
            model.share = 0;
            model.title = lecturePrePack.getPackName();
            model.userNick = lecturePrePack.getUserNick();
            model.count = lecturePrePack.getResources().size();
            model.isPack = isPack;
        } else {
            model.isPack = isPack;
        }
        return model;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

        public String id;

        public String userId;

        public String userNick;

        public String schoolId;

        public String schoolNick;

        public Long packSize;

    }

    protected class ResModel {
        public String formatId;
        public String title;
    }

    protected static class RespModel {
        public String id;
        public String title;
        public int count;
        public Integer share;
        // 贡献者学校名称
        public String schoolNick;
        // 贡献者名称
        public String userNick;
        // 创建时间（收录时间）
        public String createdDateFormat;
        // 资源
        public List<ResModel> resources;
        // 是否已经存在备课包
        public boolean isPack;

    }
}